package com.costeaalex.MIDlets.remote.canvas;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.costeaalex.MIDlets.remote.BlueMIDlet;
import com.costeaalex.MIDlets.remote.communication.CommunicationManager;

public class MouseCanvas extends Canvas implements RemoteCanvas,CommandReceiver
	{
	
	private Command cmExit;
	private Command cmBack;
	private CommunicationManager cML;
	private BlueMIDlet midlet;
	
	private CanvasListener cL;
	private String keyString=" ";
	private boolean drawImage=false;
	private byte[] imgByteL=null;
	
	private boolean initialized=false;
	
	private int id=10;
	
	private String name="Mouse";
	
	private Echoer echo;
	
	public MouseCanvas(CommunicationManager cM, BlueMIDlet midl)
		{
		
		cL=new CanvasListener();
		cmExit=new Command("Exit", Command.EXIT, 1);
		cmBack=new Command("Back", Command.BACK, 1);
		midlet=midl;
		cML=cM;
		echo=new Echoer(cML);
		echo.start();
		
		addCommand(cmExit);
		addCommand(cmBack);
		setCommandListener(cL);
		
		}
	

	protected void keyPressed(int keyCode)
		{ 
		switch(keyCode)
			{
			case 48:{cML.send(new byte[] {1, 10, 1, 0});break;}
			case 49:{cML.send(new byte[] {1, 10, 1, 1});break;}
			case 50:{cML.send(new byte[] {1, 10, 1, 2});break;}
			case 51:{cML.send(new byte[] {1, 10, 1, 3});break;}
			case 52:{cML.send(new byte[] {1, 10, 1, 4});break;}
			case 53:{cML.send(new byte[] {1, 10, 1, 5});break;}
			case 54:{cML.send(new byte[] {1, 10, 1, 6});break;}
			case 55:{cML.send(new byte[] {1, 10, 1, 7});break;}
			case 56:{cML.send(new byte[] {1, 10, 1, 8});break;}
			case 57:{cML.send(new byte[] {1, 10, 1, 9});break;}
			case 42:{cML.send(new byte[] {1, 10, 1, 11});break;}
			case 35:{cML.send(new byte[] {1, 10, 1, 12});break;}
			}
		repaint();
		}
	
	protected void keyReleased(int keyCode)
		{
		switch(keyCode)
			{
			case 48:{cML.send(new byte[] {1, 10, 2, 0});break;}
			case 49:{cML.send(new byte[] {1, 10, 2, 1});break;}
			case 50:{cML.send(new byte[] {1, 10, 2, 2});break;}
			case 51:{cML.send(new byte[] {1, 10, 2, 3});break;}
			case 52:{cML.send(new byte[] {1, 10, 2, 4});break;}
			case 53:{cML.send(new byte[] {1, 10, 2, 5});break;}
			case 54:{cML.send(new byte[] {1, 10, 2, 6});break;}
			case 55:{cML.send(new byte[] {1, 10, 2, 7});break;}
			case 56:{cML.send(new byte[] {1, 10, 2, 8});break;}
			case 57:{cML.send(new byte[] {1, 10, 2, 9});break;}
			case 42:{cML.send(new byte[] {1, 10, 2, 11});break;}
			case 35:{cML.send(new byte[] {1, 10, 2, 12});break;}
			}
		}
	
	public void paint(Graphics g)
		{
		g.setColor(255,255,255);
		g.fillRect(0, 0, 100, 100);
				
		if(drawImage)
			{
			try
				{
				Image i=Image.createImage(imgByteL, 0, imgByteL.length);
				g.drawImage(i, 0, 0, Graphics.TOP|Graphics.LEFT);
				}
			catch(Exception e)
				{
				
				}
			}
		
		g.setColor(255,0,0);
		g.drawString(keyString, 10, 10, Graphics.TOP|Graphics.LEFT);
		
		g.setColor(255,0,0);
		g.drawLine(this.getWidth()/2, this.getHeight()/2+25, this.getWidth()/2+5, this.getHeight()/2+25);
		g.drawLine(this.getWidth()/2, this.getHeight()/2+25, this.getWidth()/2, this.getHeight()/2+25+5);
		g.drawLine(this.getWidth()/2+5, this.getHeight()/2+25, this.getWidth()/2, this.getHeight()/2+25+5);
		g.drawLine(this.getWidth()/2+3, this.getHeight()/2+25+2, this.getWidth()/2+2, this.getHeight()/2+25+3);
	
		}
	
	public void drawString(String data)
		{
		keyString=data;
		repaint();	
		}
	
	public void drawImage(byte[] imgByte)
		{
		drawImage=true;
		imgByteL=imgByte;
		repaint();
		}
	
	public class CanvasListener implements CommandListener
		{

		public void commandAction(Command c, Displayable d)
			{
			if(c==cmExit)
				{
				midlet.shutDown();
				}
			if(c==cmBack)
				{
				midlet.displayManager.returnToParent();
				}
			}
		
		}

	public void initialize()
		{
		if(!initialized)
			{
			//cML.send(new byte[] {3, 10, 0, 0});
			echo.sendData(new byte[] {3, 10, 0, 0});
			//while(echo.isSending())
				//{}
			//echo.sendData(new byte[] {3, 10, 0, 0});
			drawString("iei"+echo.isSending());
			initialized=true;
			}
		}
	
	public String getName()
		{
		return name;
		}

	public int getReceiverId()
		{
		return id;
		}

	public void parseCommand(byte[] command)
		{
		if(command[0]==3)
			echo.receiveData(command);
		}

	public void parseData(byte[] data)
		{
		
		}
	
	}
